//获取url参数
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null)
        return unescape(r[2]);
    return null;
}

function showData() {
//    overlayer("open");
    $.ajax({
        type: "POST",
        async: false,
        url: "../../action/policy/get.do",
        data: {type: type, messageNo: id, instance: DepotName},
        dataType: "json",
        success: function(souData) {
            //souData = $.parseJSON(souData);
            var policy_desc = souData.Comment;
            $(".policy_desc").val(policy_desc);
            var data = souData.Content;
            var Trigger_KWListID = data.Trigger_KWListID;
            $("#Trigger_KWListID").val(Trigger_KWListID);

            var keywords = data.Trigger_KW.KWName;
            var keyword_textarea = "";
            for (var j = 0; j < keywords.length; j++) {
                var ws = keywords[j];
                keyword_textarea += ws + "\n";
            }
            $("#KWName-textarea").val(keyword_textarea);
        }
    });
}

//获取表单数据
function generateParameter() {
    var obj = {};
    obj.Trigger_KWListID = $("#Trigger_KWListID").val();
    obj.Trigger_KW = {};
    obj.Trigger_KW.KWName = [];

    var keywords = $("#KWName-textarea").val().trim().split("\n");
    for (var i = 0; i < keywords.length; i++) {
        var HostName = keywords[i];
        if (HostName != "") {
            obj.Trigger_KW.KWName.push(HostName);
        }
    }
    return obj;
}

//添加数据
function save() {
    var form = $(".form-horizontal").validate();
    if (!form.form()) {
        return;
    }
    var comment = $(".policy_desc").val();
    var policy = generateParameter();
    if (!policy) {
        return;
    }
    policy = JSON.stringify(policy);
    $.ajax({
        type: "POST",
        async: false,
        url: "../../action/policy/create.do",
        data: {comment: comment, policy: policy, type: type, instance: DepotName},
        dataType: "json",
        success: function(data) {
            if (data.result == "true") {
                alert("添加成功！");
                previous();
            } else {
                alert("添加失败!");
            }
        }
    });
}

//编辑数据
function edit() {
    var form = $(".form-horizontal").validate();
    if (!form.form()) {
        return;
    }
    var comment = $(".policy_desc").val();
    var policy = generateParameter();
    if (!policy) {
        return;
    }
    policy = JSON.stringify(policy);
    $.ajax({
        type: "POST",
        async: false,
        url: "../../action/policy/update.do",
        data: {messageNo: id, comment: comment, policy: policy, type: type, instance: DepotName},
        dataType: "json",
        success: function(data) {
            if (data.result == "true") {
                alert("编辑成功！");
                previous();
            } else {
                alert("编辑失败!")
            }
        }
    });
}

//返回上一级
function previous() {
    window.location = "../policyList.html?type=" + type + "&id=" + DepotID + "&name=" + base64.encode(DepotName) + "";
}

//$(function(){
//    setInterval(function () {
//        parent.$("#page-content").attr("height", $("html").height());
//    }, 500);
//});
/*
 * 查看旧版本的策略内容
 */
function show_oldPolicy(array, number) {
    var souData = array[number];
    $(".header").text("当前策略在" + souData.CreateTime + "创建,ID为" + souData.MessageNo + ",版本号为" + souData.MessageSerialNo + ",属于" + souData.Instance + "策略库");

    var policy_desc = souData.Comment;
    $(".policy_desc").val(policy_desc);
    var data = souData.Content;
    var Trigger_KWListID = data.Trigger_KWListID;
    $("#Trigger_KWListID").val(Trigger_KWListID);

    var keywords = data.Trigger_KW.KWName;
    var keyword_textarea = "";
    for (var j = 0; j < keywords.length; j++) {
        var ws = keywords[j];
        keyword_textarea += ws + "\n";
    }
    $("#KWName-textarea").val(keyword_textarea);
}

/*
 *获取所有版本的策略内容 
 */
function getAllPolicy() {
    $.ajax({
        type: "GET",
        async: false,
        url: "../../action/policy/history_policy_info.do",
        data: {type: type, instance: getQueryString("DepotName"), messageNo: id},
        dataType: "json",
        success: function(data) {
            policyArray = data;
            policyAmount = data.length
        }
    });
}